package com.bifrost.service;

import com.bifrost.constant.BonusPointsSource;
import com.bifrost.constant.BonusPointsUpdatedDetail;
import com.bifrost.constant.BonusPointsUpdatedType;
import com.bifrost.entity.BonusPointsHistory;
import com.bifrost.entity.Customer;
import com.bifrost.entity.MerchantStore;
import com.bifrost.model.ResultModel;
import com.bifrost.repository.BonusPointsHistoryRepository;
import com.bifrost.repository.CustomerRepository;
import com.bifrost.repository.MerchantStoreRepository;
import com.bifrost.uid.impl.DefaultUidGenerator;
import com.bifrost.util.NumberUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.math.BigInteger;
import java.util.Date;
import java.util.List;

@Service
public class ClearBonusService {
    @Autowired
    private CustomerRepository customerRepository;
    @Autowired
    private BonusPointsHistoryRepository bonusPointsHistoryRepository;
    @Autowired
    DefaultUidGenerator defaultUidGenerator;
    @Autowired
    private MerchantStoreRepository merchantStoreRepository;
    @Transactional
    public void clear(Long storeId){
        //int clear = customerRepository.checkBonusIsClear(storeId);
        MerchantStore store = merchantStoreRepository.getOne(storeId);
        List<Customer> customers = customerRepository.findCustomerByStoreId(storeId);
        if(CollectionUtils.isNotEmpty(customers)){
            for (Customer customer:customers){
                if(!BigInteger.ZERO.equals(customer.getBonusPoints())){
                    recordHistory(store,customer.getLoyaltyCard(),NumberUtils.getRandomNumber(8),null,customer.getBonusPoints(),BonusPointsUpdatedType.DECREMENT,BonusPointsUpdatedDetail.CLEAR,BonusPointsSource.OTHER,"积分清零",customer.getBonusPoints().toString());
                }
            }
            customerRepository.clearBonus(storeId);
        }
    }
    @Transactional
    public void recordHistory(MerchantStore store, String cardNumber, String orderNumber, String originNumber, BigInteger changeValue, BonusPointsUpdatedType changeType, BonusPointsUpdatedDetail detail, BonusPointsSource source, String remark, String data) {
        BonusPointsHistory history = new BonusPointsHistory();
        Customer customer = customerRepository.findFirstByLoyaltyCard(cardNumber);
        Assert.notNull(customer,cardNumber+":未找到对应会员信息");
        history.setNumber(orderNumber);
        history.setCustomerLastName(customer.getCustomerLastname());
        history.setCustomerPhone(customer.getCustomerPhone());
        history.setCardNumber(cardNumber);
        history.setMerchantId(store.getMerchantId());
        history.setStoreCode(store.getStoreCode());
        history.setOriginNumber(originNumber);
        history.setUpdatedValue(changeValue);
        history.setUpdatedType(changeType);
        history.setUpdatedTypeDetail(detail);
        history.setSource(source);
        history.setRemark(remark);
        history.setUpdatedTimestamp(new Date());
        history.setData(data);
        history.setTransactionId(defaultUidGenerator.getUID());
        bonusPointsHistoryRepository.save(history);
    }

}
